package cn.yunyichina.provider.pay.service.impl;

import cn.yunyichina.provider.framework.dubbo.service.PacityService;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.provider.iface.entity.dto.pacity.PacityUserInfoRequestDTO;
import cn.yunyichina.provider.iface.entity.dto.pacity.PacityUserInfoResponseDTO;
import cn.yunyichina.provider.pay.constant.AppType;
import cn.yunyichina.provider.pay.constant.ResponseEnum;
import cn.yunyichina.provider.pay.service.BaseUserInfoService;
import cn.yunyichina.provider.pay.service.UserInfoService;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by Lullaby on 2017/6/15.
 */
@Service
public class UserInfoServiceImpl extends BaseUserInfoService implements UserInfoService {

    private static final Logger logger = LoggerFactory.getLogger(AccessTokenServiceImpl.class);

    /**
     * 获取用户信息
     *
     * @param data
     * @return
     */
    public ResponseObject getUserInfo(String data) {
        logger.info("UserInfoServiceImpl.getUserInfo.data -> {}", data);

        ResponseObject response = new ResponseObject();

        try {
            Map<String, String> params = JSON.parseObject(data, Map.class);

            String appType = params.get("app_type");
            switch (AppType.valueOf(appType)) {
                case WECHAT:
                    response = getWechatUserInfo(params);
                    break;
                case ALIPAY:
                    response = getAlipayUserInfo(params);
                    break;
                case PACITY:
                    response = getPacityUserInfo(params);
                    break;
                default:
                    response.setResultCodeAndMessage(
                            ResponseEnum.UNSUPPORTED_APP_TYPE.getCode(),
                            ResponseEnum.UNSUPPORTED_APP_TYPE.getMessage()
                    );
                    break;
            }
        } catch (Exception e) {
            logger.error("UserInfoServiceImpl.getUserInfo.exception", e);
            response.setResultCodeAndMessage(
                    ResponseEnum.USER_INFO_EXCEPTION.getCode(),
                    ResponseEnum.USER_INFO_EXCEPTION.getMessage()
            );
        }

        return response;
    }

    /**
     * 获取微信用户信息
     *
     * @param params
     * @return
     */
    private ResponseObject getWechatUserInfo(Map<String, String> params) {
        ResponseObject response = new ResponseObject();
        logger.info("UserInfoServiceImpl.getWechatUserInfo.params -> {}", params);
        return response;
    }

    /**
     * 获取支付宝用户信息
     *
     * @param params
     * @return
     */
    private ResponseObject getAlipayUserInfo(Map<String, String> params) {
        ResponseObject response = new ResponseObject();
        logger.info("UserInfoServiceImpl.getAlipayUserInfo.params -> {}", params);
        return response;
    }

    /**
     * 获取平安平台用户信息
     *
     * @param params
     * @return
     */
    private ResponseObject getPacityUserInfo(Map<String, String> params) {
        ResponseObject response = new ResponseObject();

        if (!checkGetPacityUserInfoParameters(params))
            return response.buildResultCodeAndMessage(
                    ResponseEnum.Common.INVALID.getCode(),
                    ResponseEnum.Common.INVALID.getMessage()
            );

        PacityUserInfoRequestDTO requestDTO = buildGetPacityUserInfoRequestDTO(params);
        logger.info("UserInfoServiceImpl.getPacityUserInfo.requestDTO -> {}", JSON.toJSONString(requestDTO));

        ResCommon<PacityUserInfoResponseDTO> userinfoResponse = PacityService.getPacityClient().getPacityUserInfo(requestDTO);
        logger.info("UserInfoServiceImpl.getPacityUserInfo.userinfoResponse -> {}", JSON.toJSONString(userinfoResponse));

        PacityUserInfoResponseDTO pacityUserInfo = userinfoResponse.getResult();
        logger.info("UserInfoServiceImpl.getPacityUserInfo.pacityUserInfo -> {}", JSON.toJSONString(pacityUserInfo));

        if (userinfoResponse.isSuccess()) {
            response.setResultObject(pacityUserInfo);
        } else {
            response.setResultCodeAndMessageAndResult(
                    ResponseEnum.PACITY_USER_INFO_FAIL.getCode(),
                    ResponseEnum.PACITY_USER_INFO_FAIL.getMessage(),
                    pacityUserInfo
            );
        }

        return response;
    }

}
